package cn.big.bigd.biocode.admin.controller;

import cn.big.bigd.biocode.dao.CurationDAO;
import cn.big.bigd.biocode.dao.ToolDAO;
import cn.big.bigd.biocode.dao.UserDAO;
import cn.big.bigd.biocode.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller(value = "adminController")
@RequestMapping("admin")
public class AdminController {
    @Autowired
    CurationDAO curationDAO;
    @Autowired
    UserDAO userDAO;
    @Autowired
    ToolDAO toolDAO;


    @RequestMapping(value = {"home"})
    public String home(HttpServletRequest request , HttpServletResponse response,Model model) {
        HttpSession session = request.getSession();
        User user =(User) session.getAttribute("currentUser");
        System.out.println(user.getEmail());
        if(null==user){
            response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
        }
        model.addAttribute("tools",toolDAO.findByUser(user));


        return "admin/home";
    }

    @RequestMapping(value = {"tools"})
    public String tools(Model model, Integer id, HttpServletRequest request, HttpServletResponse response) {

        HttpSession session = request.getSession();
        User user =(User) session.getAttribute("currentUser");
        System.out.println(user.getEmail());
        if(null==user){
            response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
        }
        model.addAttribute("tools",toolDAO.findByUser(user));


        return "user/tools";
    }



    @RequestMapping(value = {"users"})
    public String tools(Model model,  HttpServletRequest request, HttpServletResponse response) {
      List<User> userList =userDAO.findCurationUsers();
        System.out.println(userList.size());
        model.addAttribute("users", userList);
        return "admin/users";
    }
}
